<?php

/**
 * 积分兑换 
 * File Name：RedeemController.php
 * File Encoding：UTF-8
 * File New Time：2014-6-14 9:35:38
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class RedeemgoodController extends AdminBase {


    //商品管理
    public function actionIndex() 
    {
        $objModel = new RedeemModel('search');
        $objModel->resetAttributes();

        $type = isset($_GET['type']) ? $_GET['type'] : '';
        $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';

        if(isset($_GET['RedeemModel']) || $type)
        {
            switch ($type) {
                case 1:
                    $_GET['RedeemModel']['title'] = $_GET['keyword'];
                    break;
                case 2:
                    $_GET['RedeemModel']['product_id'] = $_GET['keyword'];
                    break;
                case 3:
                    $_GET['RedeemModel']['uid'] = $_GET['keyword'];
                    break;
            }
            $objModel->attributes = $_GET['RedeemModel'];
        }

        $objCriteria = $objModel->createSearchCriteria();


        $intCount = RedeemModel::model()->count($objCriteria);

        //使用后台分页
        
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        $limit = 20;

        $page = self::page($intCount, $limit, $pageId);

        $objCriteria->limit = $page->listRows;
        $objCriteria->offset = $page->firstRow;

        //排序
        $objSort = new CSort( 'RedeemModel' );
        $objSort->defaultOrder = 'pay_status DESC';
        $objSort->applyOrder( $objCriteria );

        $this->assign('type', $type);
        $this->assign('keyword', $keyword);

        $objModels =  RedeemModel::model()->findAll($objCriteria);
        $this->render('index',array('Page' => $page->show(),'data' => $objModels,'count' => $intCount, 'model' => $objModel));       

    }



    public function actionAdd() {
        $model = RedeemModel::model();
        $model->member_group = '1,2,3';//默认全部勾选      
        if (IS_POST) {

            if ($model->add($_POST)) {
                $this->success('添加成功！', self::U('index'));
            } else {
                $error = $model->getOneError();
                $this->error($error ? $error : '添加失败！');
            }
        } else {
            $this->assign('data', $model);
            $this->render();
        }
    }

    public function actionEdit() {
        $model = RedeemModel::model();
        if (IS_POST) {
            if ($model->editor($_POST)) {
                $this->success('修改成功！', self::U('index'));
            } else {
                $error = $model->getOneError();
                $this->error($error ? $error : '修改失败！');
            }
        } else {
            $this->baseEdit($model, 'index');
        }
    }

    public function actionDelete() {
        $this->error('删除功能关闭！');
        $model = RedeemModel::model();
        $this->baseDelete($model, 'index');
    }

    public function actionRefund()
    {
        $id = Yii::app()->request->getParam('id');

        $objModel = RedeemModel::model()->findByPk($id);
        if(!$objModel)
        {
            $this->error('找不到此记录');
        }

        if($objModel->pay_status == RedeemModel::PAY_STATUS_REFUND_NOT)
        {
            $objModel->pay_status = RedeemModel::PAY_STATUS_REFUND_HAS;
            $objModel->save(false, array('pay_status'));
            $this->success('退款完成');
        }else
        {
            $this->error('不能进行此操作');
        }      
    }

    public function actionRecord()
    {   
        $objModel = new RedeemRecordsModel('search');
        $objModel->resetAttributes();

        $arrFormData['keyword'] = isset($_GET['keyword']) ? $_GET['keyword'] : '';
        $arrFormData['type'] = isset($_GET['type']) ? $_GET['type'] : '';

        //NODE
        //$arr_with = array('MembersModel', 'RedeemModel', 'MembersModel_seller', 'MembersInfoModel_seller');
        $arr_with = array('MembersModel', 'RedeemModel');
        //$arr_with = array();
        if(isset($_GET['RedeemRecordsModel']))
        {
            switch ($arrFormData['type']) {
                case 1:
                    $_GET['RedeemRecordsModel']['username'] = $arrFormData['keyword'];
                    break;
                case 2:
                    $_GET['RedeemRecordsModel']['uid'] = $arrFormData['keyword'];
                    break;
                case 3:
                    if($arrFormData['keyword'])
                    {
                        unset($arr_with[1]);
                        $arr_with['RedeemModel'] = array('condition' => 'RedeemModel.product_id = "'. $arrFormData['keyword'].'"');
                    }                    
                    break; 
                case 4:
                    $_GET['RedeemRecordsModel']['order_sn'] = $arrFormData['keyword'];
                    break;
            }
            $objModel->attributes = $_GET['RedeemRecordsModel'];
        }
        $objCriteria = $objModel->createSearchCriteria();

        $this->assign($_GET);
        $intCount = RedeemRecordsModel::model()->with($arr_with)->count($objCriteria);
        

        //使用后台分页
        
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        $limit = 20;

        $page = self::page($intCount, $limit, $pageId);

        $objCriteria->limit = $page->listRows;
        $objCriteria->offset = $page->firstRow;

        //排序
        $objSort = new CSort( 'RedeemRecordsModel' );
        $objSort->defaultOrder = 't.id DESC';
        $objSort->applyOrder( $objCriteria );

        $objModels =  RedeemRecordsModel::model()->with($arr_with)->findAll($objCriteria);

        $this->render('record',array('sort' => $objSort,'formData' => $arrFormData, 'Page' => $page->show(),'data' => $objModels,'count' => $intCount, 'model' => $objModel));       

    }
    public function actionRecordEdit() 
    {
        $id = Yii::app()->request->getParam('id');

        $objModel = RedeemRecordsModel::model()->findByPk($id);
        if(!$objModel)
        {
            $this->error('找不到此记录','index');
        }

        if( IS_POST )
        {   
            //赋值
            $objModel->attributes = $_POST;
        
            if($objModel->validate() && $objModel->save())
            {                
                $objRedeemModel = RedeemModel::model()->findByPk($objModel->redeem_id);
                if($objRedeemModel)
                {
                    $quantum = $objRedeemModel->getRecordQuantum(true);

                    if($quantum >= $objRedeemModel->quantum)
                    {
                        if(!$objRedeemModel->end_time)
                        {
                            $objRedeemModel->end_time = time() + 86400 * 3;
                            $objRedeemModel->save(false,array('end_time'));  
                        }                     
                    }else
                    {
                        if($objRedeemModel->end_time)
                        {
                            $objRedeemModel->end_time = 0;
                            $objRedeemModel->save(false,array('end_time'));  
                        }                            
                    }                        
                }
                $this->success('修改成功');           
            }else
            {
                $error = $objModel->getOneError();
                $this->error($error ? $error : '修改失败！');              
            }
        }
    }
    //快捷编辑后，ajax重新加载内容
    public function actionPublic_load() {
        $id = Yii::app()->request->getParam('id');
        //查询报名信息
        $data = RedeemModel::model()->findByPk($id);
        if (empty($data)) {
            exit('');
        }
        $this->assign('rs', $data);
        $this->render();
    }
    /**
     * 订单记录
     * @return [type] [description]
     */
    //订单查询
    public function actionOrders() {
        //搜索
        if (IS_POST) {
            $this->redirect(AdminBase::U('orders', $_POST));
            exit;
        }
        $where = array(
            'order' => 'add_time desc'
        );
        $where['payment_types'] = array('EQ', 8);
        //状态
        $status = $_GET['status'];
        if ($status != '') {
            $where['status'] = array('EQ', (int) $status);
            $this->assign('status', $status);
        }
        //搜索类型
        $type = (int) $_GET['type'];
        $keyword = trim($_GET['keyword']);
        if ($keyword) {
            $this->assign('type', $type);
            $this->assign('keyword', $keyword);
            if ($type == 1) {
                $where['product_id'] = array('EQ', $keyword);
            } else if ($type == 2) {
                $where['order_sn'] = array('EQ', $keyword);
            } else if ($type == 3) {
                $where['alipay_sn'] = array('EQ', $keyword);
            }
        }

        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 20;
        //订单模型
        $model = MoneyPositionOrderModel::model();
        $criteria = $model->where($where);
        //信息总数
        $count = $model->count($criteria);
        //分页
        $page = AdminBase::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $model->findAll($criteria);

        $this->assign("Page", $page->show());
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->assign('where', $where);
        $this->render();
    }

    //信息通知
    public function actionNotice() {
        $notice = Yii::app()->request->getParam('notice');
        $uid = (int)Yii::app()->request->getParam('uid');
        if (empty($notice)) {
            $this->error('通知内容不能为空！');
        }
        
        $userInfo = MembersModel::model()->getUserInfo($uid);
        if (empty($userInfo['phone'])) {
            $this->error('手机号为空，无法发送！');
        }

        Sns::getInstance()->mobile($userInfo['phone'])->data($notice)->send();
        $this->success('发送成功！');
    }

    public function actionPublic_ShowAddress(){
        $uid = Yii::app()->request->getParam('uid');
        $model = MembersAddressModel::model()->getMemberAddres($uid);
        if($model){
            $info = $model->getAddressDetail();
        }else{
            $info = '客户未填联系方式';
        }
        $this->success($info);
    }

}
